package bmptest.bmp.controller.SysRole;

import bmptest.bmp.entity.SysMenu;
import bmptest.bmp.entity.SysMenuRole;
import bmptest.bmp.entity.SysRole;
import bmptest.bmp.service.SysMenuRole.SysMenuRoleService;
import bmptest.bmp.service.SysRole.SysRoleService;
import bmptest.bmp.service.Sysmenu.SysMenuService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author lyq
 * @date 2021/9/6 9:49
 * 角色管理模块
 */

@RestController
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    //多条件搜索
    @CrossOrigin
    @RequestMapping(value = "/api/queryRolesList",method = RequestMethod.GET)
    public Map<String, Object> searchUser(@RequestParam("role_name") String rolename,
                                          @RequestParam("begindate") String begindate, @RequestParam("enddate") String enddate,
                                          @RequestParam("pageNum") String pageNum, @RequestParam("pageSize") String pageSize){
        List<SysRole> pagelist = sysRoleService.searchRole(rolename,begindate,enddate);
        Integer pn = Integer.parseInt(pageNum);
        Integer ps = Integer.parseInt(pageSize);
        if(!"null".equals(String.valueOf(pn))&&!"null".equals(String.valueOf(ps))){
            PageHelper.startPage(pn, ps);
        }
        List<SysRole> page = sysRoleService.searchRole(rolename,begindate,enddate);
        Integer size = pagelist.size();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pagelist",page);
        map.put("totalsize",size);
        return map;
    }

    //新增
    @CrossOrigin
    @RequestMapping(value = "/api/addRoles", method = RequestMethod.POST)
    public String  addUser(@RequestBody SysRole sRole) throws Exception{
        String isture = sysRoleService.addAll(sRole);
        return isture;
    }

    //删除
    @CrossOrigin
    @RequestMapping(value = "/api/deleteRoles",method = RequestMethod.POST)
    public void deleteUser(@RequestBody SysRole sysRole){
        sysRoleService.deleteRole(sysRole.getRole_id());
    }

    //更新
    @CrossOrigin
    @RequestMapping(value = "/api/updateRoles",method = RequestMethod.POST)
    public Map<String, Object> updateUser(@RequestBody SysRole sysRole){
        String isvalidate = sysRoleService.updateRole(sysRole);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sysRole",sysRole);
        map.put("isvalidate",isvalidate);
        return map;
    }

    //查询菜单
    @Autowired
    private SysMenuRoleService sysMenuRoleService;

    //根据roleid查询菜单
    @CrossOrigin
    @RequestMapping(value = "/api/querySysMenuByRole",method = RequestMethod.GET)
    public List<SysMenuRole> querySysMenu(@RequestParam("role_id") String roleid){
        List<SysMenuRole> listMenuByRole = sysMenuRoleService.queryMenuByRole(roleid);
        return listMenuByRole;
    }

    //查询所有菜单
    @CrossOrigin
    @RequestMapping(value = "/api/queryAllMenu",method = RequestMethod.GET)
    public List<SysMenu> queryAllMenu(){
        List<SysMenu> listallmenu = sysMenuService.querySysMenu();
        List<SysMenu> treemenu = sysMenuService.buildtree(listallmenu,null);
        return treemenu;
    }

    /**
     * @author lyq
     * @date 2021/9/23 8:58
     * 提交修改的角色菜单
     */
    @CrossOrigin
    @RequestMapping(value = "/api/updateRoleMenu",method = RequestMethod.POST)
    public String updateRoleMenu(@RequestBody Map<String, Object> param){
        Object rid = param.get("role_id");
        String roleid = (String)rid;
        List<String> sysMenuRoleByRoleId = sysMenuRoleService.getMenuIdByRoleId(roleid);

        //obj转list
        Object menuidlist = param.get("menu_id");
        List<String> menuid = new ArrayList<>();
        if (menuidlist instanceof ArrayList<?>) {
            for (Object o : (List<?>) menuidlist) {
                menuid.add(String.class.cast(o));
            }
        }
        //根据更新的菜单id数量判断增删
        if(menuid.size()!=0&&menuid.size()==sysMenuRoleByRoleId.size()){
            return "0";
        }else if(menuid.size()!=0&&menuid.size()>sysMenuRoleByRoleId.size()){
            sysMenuRoleService.addNewSysMenuRole(menuid,sysMenuRoleByRoleId,roleid);
            return "1";
        }else if(menuid.size()!=0&&menuid.size()<sysMenuRoleByRoleId.size()){
            sysMenuRoleService.deleteSysMenuRole(menuid,sysMenuRoleByRoleId,roleid);
            return "1";
        }
        return "0";
    }


}
